<script>
import {
  addFrequentlyUsedURLs,
  delFrequentlyUsedURLs,
  getFrequentlyUsedURLsAll, updateFrequentlyUsedURLs,
} from "@/api/myDesktop/frequentlyUsedURLs/frequentlyUsedURLs";
import Cookies from "js-cookie";

export default {
  name: "frequentlyUsedURLs",
  data() {
    return {
      pageNum: 1, // 当前页
      pageSize: 5, // 每页显示数量
      pages: 0, // 总页数
      updateFrequentlyUsedURLsVisible: false,
      addFrequentlyUsedURLsVisible: false,
      data: {},
      tableData: [{
        userID: null,
        urlName: null,
        url: null
      }]
    }
  },
  methods: {
    getData: function(pageNum, pageSize) {
      let user = JSON.parse(Cookies.get("SET_USERS"));
      getFrequentlyUsedURLsAll(pageNum, pageSize, user.userID).then(res => {
        if (res.data != null) {
          this.tableData = res.data.list;
          this.pageNum = res.data.pageNum;
          this.pageSize = res.data.pageSize;
          this.pages = res.data.total;
        }
      })
    },
    handleSizeChange: function (val) {
      this.pageSize = val;
      this.getData(this.pageNum, this.pageSize);
    },
    handleCurrentChange: function (val) {
      this.pageNum = val;
      this.getData(this.pageNum, this.pageSize);
    },
    edit: function(data, boolean) {
      this.data = data;
      this.updateFrequentlyUsedURLsVisible = boolean;
    },
    del: function(id, boolean) {
      this.updateFrequentlyUsedURLsVisible = boolean;
      delFrequentlyUsedURLs(id).then(res => {
        if (res.success === true) {
          this.$message.success(res.msg);
        }
      })
    },
    onSubmit: function(data) {
      console.log(data)
      updateFrequentlyUsedURLs(data).then(res => {
        if (res.success === true) {
          this.$message.success(res.msg);
          this.updateFrequentlyUsedURLsVisible = false;

        }
      })
    },
    onSubmit2: function(data) {
      console.log(data)
      let user = JSON.parse(Cookies.get("SET_USERS"));
      data.userID = user.userID;
      addFrequentlyUsedURLs(data).then(res => {
        if (res.success === true) {
          this.$message.success(res.msg);
          this.updateFrequentlyUsedURLsVisible = false;
          location.reload();
        }
      })
    }
  },
  mounted() {
    this.getData(this.pageNum, this.pageSize);
  }
}
</script>

<template>
  <div>
    <el-button @click="addFrequentlyUsedURLsVisible = true">新增网址</el-button>
    <el-table
      :data="tableData"
      border
      style="width: 100%">
      <el-table-column
        prop="urlName"
        label="网址名称"
        width="180">
      </el-table-column>
      <el-table-column
        label="网址">
        <template slot-scope="scope">
          <a :href="scope.row.url" target="_blank">{{scope.row.url}}</a>
        </template>
      </el-table-column>
      <el-table-column
        width="180px"
        label="操着">
        <template slot-scope="scope">
          <el-button
            style="float: left"
            size="mini"
            @click="edit(scope.row ,true)">编辑</el-button>
          <el-button
            style="float: left"
            type="danger"
            size="mini"
            @click="del(scope.res.id, true)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[5, 10, 25]"
        :page-size="this.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.pages">
      </el-pagination>
    </div>

    <!--    编辑常用网址-->
    <el-dialog
      title="常用网址"
      :visible.sync="updateFrequentlyUsedURLsVisible"
      width="40%"
      :append-to-body="true">
      <div>
        <el-form ref="form" :model="data" label-width="80px">
          <el-form-item label="网址名称">
            <el-input v-model="data.urlName"></el-input>
          </el-form-item>
          <el-form-item label="便签内容">
            <el-input type="textarea" v-model="data.url"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit(data)">保存</el-button>
            <el-button @click="updateFrequentlyUsedURLsVisible = false">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
    <!--    编辑常用网址-->
    <el-dialog
      title="常用网址"
      :visible.sync="addFrequentlyUsedURLsVisible"
      width="40%"
      :append-to-body="true">
      <div>
        <el-form ref="form" :model="data" label-width="80px">
          <el-form-item label="网址名称">
            <el-input v-model="data.urlName"></el-input>
          </el-form-item>
          <el-form-item label="便签内容">
            <el-input type="textarea" v-model="data.url"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit2(data)">保存</el-button>
            <el-button @click="addFrequentlyUsedURLsVisible = false">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<style scoped lang="scss">
  .el-table,.block,.el-button {
    margin: 10px;
  }
</style>
